//
//  UIColor+Extension.swift
//  HSK-swift-2016
//
//  Created by 刘培壮 on 16/6/1.
//  Copyright © 2016年 上海实奥信息技术有限公司. All rights reserved.
//

import UIKit

/**
 * 扩展UIColor的便利构造方法
 */
extension UIColor {
    
    convenience init(red : CGFloat, green : CGFloat, blue : CGFloat) {
         self.init(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: 1)
    }

    convenience init(RGB: CGFloat) {
        self.init(red: RGB, green: RGB, blue: RGB)
    }

    convenience init(hex: String, alpha:CGFloat) {
        var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        
        if (cString.hasPrefix("#")) {
            cString = (cString as NSString).substring(from: 1)
        }
        else if (cString.hasPrefix("0X") || cString.hasPrefix("0x")) {
            cString = (cString as NSString).substring(from: 2)
        }
        
        if ((cString as NSString).length != 6) {
            
        }
        
        let rString = (cString as NSString).substring(to: 2)
        let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
    }
    
    convenience init(hex:String) {
        self.init(hex:hex,alpha:1)
    }
    
}

    // MARK: - 颜色的一些操作
extension UIColor {
    
    //    随机颜色
    open class var random : UIColor {
        get {
            let hue = CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX))
            let saturation = CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX))
            let brightness = CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX))
            
            return UIColor(red: hue, green: saturation, blue: brightness, alpha: 1)
        }
    }
}
    // MARK: - 常用的颜色值
extension UIColor {

    open class var theme : UIColor {
        get {
            return .orange
        }
    }
    
}

